package com.neuedu.oasystem.service.impl;
/**
 * @author LHB
 */
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import com.neuedu.oasystem.dao.IAssetsDao;
import com.neuedu.oasystem.dao.impl.AssetsDaoImpl;
import com.neuedu.oasystem.entity.Assets;
import com.neuedu.oasystem.service.AssetsService;
import com.neuedu.oasystem.util.DBHelper;

public class AssetsServiceImpl implements AssetsService{
	private IAssetsDao assetsdao;
	
	public AssetsServiceImpl() {
		super();
		this.assetsdao = new AssetsDaoImpl();
	}

	@Override
	public boolean add(Assets param) {
		Connection conn = DBHelper.openConnection();
		try {
			conn.setAutoCommit(false);
			assetsdao.setConnection(conn);
			boolean ret = assetsdao.add(param);
			conn.commit();
			conn.setAutoCommit(true);
			return ret;
		} catch (SQLException e) {
			if (conn!=null) {
				try {
					conn.rollback();
					conn.setAutoCommit(true);
				} catch (SQLException e1) {
					
					e1.printStackTrace();
				}
				
			}
			e.printStackTrace();
		} finally {
			DBHelper.closeAll(null, null, conn);
		}
		return false;
	}

	@Override
	public boolean edit(Assets param) {
		Connection conn = DBHelper.openConnection();
		try {
			conn.setAutoCommit(false);
			assetsdao.setConnection(conn);
			boolean ret = assetsdao.edit(param);
			conn.commit();
			conn.setAutoCommit(true);
			return ret;
		} catch (SQLException e) {
			if (conn!=null) {
				try {
					conn.rollback();
					conn.setAutoCommit(true);
				} catch (Exception e2) {
				}
			} 
			e.printStackTrace();
		} finally {
			DBHelper.closeAll(null, null, conn);
		}
		return false;
	}

	@Override
	public boolean deleteById(int id) {
		Connection conn = DBHelper.openConnection();
		try {
			conn.setAutoCommit(false);
			assetsdao.setConnection(conn);
			boolean ret = assetsdao.deleteById(id);
			conn.commit();
			conn.setAutoCommit(true);
			return ret;
		} catch (SQLException e) {
			if (conn!=null) {
				try {
					conn.rollback();
					conn.setAutoCommit(true);
				} catch (Exception e2) {
				}
			} 
			e.printStackTrace();
		} finally {
			DBHelper.closeAll(null, null, conn);
		}
		return false;
	}

	@Override
	public Assets findById(int id) {
		Connection conn = DBHelper.openConnection();
		try {
			conn.setAutoCommit(false);
			assetsdao.setConnection(conn);
			Assets assets = assetsdao.findById(id);
			conn.commit();
			conn.setAutoCommit(true);
			return assets;
		} catch (SQLException e) {
			if (conn!=null) {
				try {
					conn.rollback();
					conn.setAutoCommit(true);
				} catch (SQLException e1) {
					e1.printStackTrace();
				}
				
			}
			e.printStackTrace();
		} finally {
			DBHelper.closeAll(null, null, conn);
		}
		return null;
	}

	@Override
	public List<Assets> findAll() {
		Connection conn = DBHelper.openConnection();
		try {
			assetsdao.setConnection(conn);
			List<Assets> list = assetsdao.findAll();
			return list;
		} catch (Exception e) {
		} finally {
			DBHelper.closeAll(null, null, conn);
		}
		
		return null;
	}

	@Override
	public List<Assets> findByExample(Assets param) {
		Connection conn = DBHelper.openConnection();
		try {
			assetsdao.setConnection(conn);
			List<Assets> list = assetsdao.findByExample(param);
			return list;
		} catch (Exception e) {
		} finally {
			DBHelper.closeAll(null, null, conn);
		}
		return null;
	}

	@Override
	public List<Assets> findByPager(int page, int size) {
		Connection conn = DBHelper.openConnection();
		try {
			assetsdao.setConnection(conn);
			List<Assets> list = assetsdao.findByPager(page, size);
			return list;
		} catch (Exception e) {
		} finally {
			DBHelper.closeAll(null, null, conn);
		}
		return null;
	}

	@Override
	public int deleteByIds(int[] ids) {
		Connection conn = DBHelper.openConnection();
		try {
			assetsdao.setConnection(conn);
			int id  = assetsdao.deleteByIds(ids);
			return id;
		} catch (Exception e) {
		} finally {
			DBHelper.closeAll(null, null, conn);
		}
		return 0;
	}

	@Override
	public int addAll(List<Assets> param) {
		Connection conn = DBHelper.openConnection();
		try {
			assetsdao.setConnection(conn);
			int all  = assetsdao.addAll(param);
			return all;
		} catch (Exception e) {
		} finally {
			DBHelper.closeAll(null, null, conn);
		}
		return 0;
	}

	@Override
	public int editAll(List<Assets> param) {
		Connection conn = DBHelper.openConnection();
		try {
			assetsdao.setConnection(conn);
			int all  = assetsdao.editAll(param);
			return all;
		} catch (Exception e) {
		} finally {
			DBHelper.closeAll(null, null, conn);
		}
		return 0;
	}

}
